package com.cqs.leetcode.string;

/**
 * lixw
 * 2023/4/1
 **/
public class MaskingPersonaInfo831 {
    public String maskPII(String s) {
        if (s.contains("@")) {
            return handleEmail(s);
        }
        return handlePhone(s);
    }

    private String handlePhone(String phone) {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < phone.length(); ++i) {
            char c = phone.charAt(i);
            //数字
            if (c >= '0' && c <= '9') {
                s.append(c);
            }
        }
        for (int i = 0; i < s.length() - 4; i++) {
            s.replace(i, i + 1, "*");
        }
        if (s.length() > 10) {
            s.insert(0, '+');
        }
        s.insert(s.length() - 4, '-');
        s.insert(s.length() - 8, '-');
        if (s.length() > 12) {
            s.insert(s.length() - 12, '-');
        }
        return s.toString();
    }

    private String handleEmail(String email) {
        String s = email.toLowerCase();
        int i = email.indexOf("@");
        return s.charAt(0) + "*****" + s.substring(i - 1);
    }

    public static void main(String[] args) {
        MaskingPersonaInfo831 m = new MaskingPersonaInfo831();
        System.out.println(m.maskPII("AB@qq.com"));
        System.out.println(m.maskPII("LeetCode@LeetCode.com"));
        System.out.println(m.maskPII("1(234)567-890"));
        System.out.println(m.maskPII("12(234)567-890"));
        System.out.println(m.maskPII("123(234)567-890"));
    }
}
